<?php


class tx_zlib_viewhelper_htmlTransform extends tx_zlib_abstract_viewhelper
{

	/**
	 * Transforms HTML content. This is the shortform of htmlTransform()->linksToHtml().
	 *
	 * If no arguments are passed, returns a tx_zlib_viewhelper_htmlTransform object.
	 *
	 * @param string $input
	 * @return string|tx_zlib_viewhelper_htmlTransform
	 */
	public function htmlTransform($input = null)
	{
        if (0 == func_num_args()) {
            return $this;
        }
		return $this->linksToHtml($input);
	}


	/**
	 * Parses URLs and E-Mail addresses and transforms them to valid HTML tags
	 *
	 * @param string $input
	 * @return string
	*/
	public function linksToHtml($str)
	{
		$from	= array();
		$to		= array();
		foreach(preg_split('#\s#', $str) AS $word) {
			$word = trim($word);
			if(substr($word, 0, 7) == 'http://') {
				$from[$word]    = $word;
				$to[$word]      = sprintf('<a target="_blank" href="%s">%s</a>', $word, $word);
			}
			else if(substr($word, 0, 4) == 'www.') {
				$from[$word]    = $word;
				$to[$word]      = sprintf('<a target="_blank" href="http://%s">%s</a>', $word, $word);
			}
			else if(t3lib_div::validEmail($word)) {
				$from[$word]    = $word;
				$to[$word]      = $this->getPi()->cObj->getTypoLink($word, $word);
			}
		}
		if((count($from) > 0) && (count($to) > 0)) {
			return str_replace($from, $to, $str);
		}
		return $str;
	}

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_htmlTransform.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_htmlTransform.php']);
}
